home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / FLEX-TC_ / SYM.C < prev    next >
Text File  |  1990-01-03  |  7KB  |  317 lines

  1. /* sym - symbol table routines */
  2.  
  3. /*
  4.  * Copyright (c) 1989 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Vern Paxson.
  9.  * 
  10.  * The United States Government has rights in this work pursuant to
  11.  * contract no. DE-AC03-76SF00098 between the United States Department of
  12.  * Energy and the University of California.
  13.  *
  14.  * Redistribution and use in source and binary forms are permitted
  15.  * provided that the above copyright notice and this paragraph are
  16.  * duplicated in all such forms and that any documentation,
  17.  * advertising materials, and other materials related to such
  18.  * distribution and use acknowledge that the software was developed
  19.  * by the University of California, Berkeley.  The name of the
  20.  * University may not be used to endorse or promote products derived
  21.  * from this software without specific prior written permission.
  22.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  23.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  24.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  25.  */
  26.  
  27. #ifndef lint
  28.  
  29. static char copyright[] =
  30.     "@(#) Copyright (c) 1989 The Regents of the University of California.\n";
  31. static char CR_continuation[] = "@(#) All rights reserved.\n";
  32.  
  33. static char rcsid[] =
  34.     "@(#) $Header: sym.c,v 2.0 89/06/20 15:50:17 vern Locked $ (LBL)";
  35.  
  36. #endif
  37.  
  38. #include "flexdef.h"
  39. #ifdef THINK_C
  40. #include <stdlib.h>
  41. #endif
  42.  
  43. struct hash_entry *ndtbl[NAME_TABLE_HASH_SIZE];
  44. struct hash_entry *sctbl[START_COND_HASH_SIZE];
  45. struct hash_entry *ccltab[CCL_HASH_SIZE];
  46.  
  47. struct hash_entry *findsym();
  48.  
  49.  
  50. /* addsym - add symbol and definitions to symbol table
  51.  *
  52.  * synopsis
  53.  *    char sym[], *str_def;
  54.  *    int int_def;
  55.  *    hash_table table;
  56.  *    int table_size;
  57.  *    0 / -1 = addsym( sym, def, int_def, table, table_size );
  58.  *
  59.  * -1 is returned if the symbol already exists, and the change not made.
  60.  */
  61.  
  62. int addsym( sym, str_def, int_def, table, table_size )
  63. register char sym[];
  64. char *str_def;
  65. int int_def;
  66. hash_table table;
  67. int table_size;
  68.  
  69.     {
  70.     int hash_val = hashfunct( sym, table_size );
  71.     register struct hash_entry *sym_entry = table[hash_val];
  72.     register struct hash_entry *new_entry;
  73.     register struct hash_entry *successor;
  74. #ifndef THINK_C
  75.     char *malloc();
  76. #endif
  77.  
  78.     while ( sym_entry )
  79.     {
  80.     if ( ! strcmp( sym, sym_entry->name ) )
  81.         { /* entry already exists */
  82.         return ( -1 );
  83.         }
  84.     
  85.     sym_entry = sym_entry->next;
  86.     }
  87.  
  88.     /* create new entry */
  89.     new_entry = (struct hash_entry *) malloc( sizeof( struct hash_entry ) );
  90.  
  91.     if ( new_entry == NULL )
  92.     flexfatal( "symbol table memory allocation failed" );
  93.  
  94.     if ( (successor = table[hash_val]) )
  95.     {
  96.     new_entry->next = successor;
  97.     successor->prev = new_entry;
  98.     }
  99.     else
  100.     new_entry->next = NULL;
  101.  
  102.     new_entry->prev = NULL;
  103.     new_entry->name = sym;
  104.     new_entry->str_val = str_def;
  105.     new_entry->int_val = int_def;
  106.  
  107.     table[hash_val] = new_entry;
  108.  
  109.     return ( 0 );
  110.     }
  111.  
  112.  
  113. /* cclinstal - save the text of a character class
  114.  *
  115.  * synopsis
  116.  *    char ccltxt[];
  117.  *    int cclnum;
  118.  *    cclinstal( ccltxt, cclnum );
  119.  */
  120.  
  121. cclinstal( ccltxt, cclnum )
  122. char ccltxt[];
  123. int cclnum;
  124.  
  125.     {
  126.     /* we don't bother checking the return status because we are not called
  127.      * unless the symbol is new
  128.      */
  129.     char *copy_string();
  130.  
  131.     (void) addsym( copy_string( ccltxt ), (char *) 0, cclnum,
  132.            ccltab, CCL_HASH_SIZE );
  133.     }
  134.  
  135.  
  136. /* ccllookup - lookup the number associated with character class text
  137.  *
  138.  * synopsis
  139.  *    char ccltxt[];
  140.  *    int ccllookup, cclval;
  141.  *    cclval/0 = ccllookup( ccltxt );
  142.  */
  143.  
  144. int ccllookup( ccltxt )
  145. char ccltxt[];
  146.  
  147.     {
  148.     return ( findsym( ccltxt, ccltab, CCL_HASH_SIZE )->int_val );
  149.     }
  150.  
  151.  
  152. /* findsym - find symbol in symbol table
  153.  *
  154.  * synopsis
  155.  *    char sym[];
  156.  *    hash_table table;
  157.  *    int table_size;
  158.  *    struct hash_entry *sym_entry, *findsym();
  159.  *    sym_entry = findsym( sym, table, table_size );
  160.  */
  161.  
  162. struct hash_entry *findsym( sym, table, table_size )
  163. register char sym[];
  164. hash_table table;
  165. int table_size;
  166.  
  167.     {
  168.     register struct hash_entry *sym_entry = table[hashfunct( sym, table_size )];
  169.     static struct hash_entry empty_entry =
  170.     {
  171.     (struct hash_entry *) 0, (struct hash_entry *) 0, NULL, NULL, 0,
  172.     } ;
  173.  
  174.     while ( sym_entry )
  175.     {
  176.     if ( ! strcmp( sym, sym_entry->name ) )
  177.         return ( sym_entry );
  178.     sym_entry = sym_entry->next;
  179.     }
  180.  
  181.     return ( &empty_entry );
  182.     }
  183.  
  184.     
  185. /* hashfunct - compute the hash value for "str" and hash size "hash_size"
  186.  *
  187.  * synopsis
  188.  *    char str[];
  189.  *    int hash_size, hash_val;
  190.  *    hash_val = hashfunct( str, hash_size );
  191.  */
  192.  
  193. int hashfunct( str, hash_size )
  194. register char str[];
  195. int hash_size;
  196.  
  197.     {
  198.     register int hashval;
  199.     register int locstr;
  200.  
  201.     hashval = 0;
  202.     locstr = 0;
  203.  
  204.     while ( str[locstr] )
  205.     hashval = ((hashval << 1) + str[locstr++]) % hash_size;
  206.  
  207.     return ( hashval );
  208.     }
  209.  
  210.  
  211. /* ndinstal - install a name definition
  212.  *
  213.  * synopsis
  214.  *    char nd[], def[];
  215.  *    ndinstal( nd, def );
  216.  */
  217.  
  218. ndinstal( nd, def )
  219. char nd[], def[];
  220.  
  221.     {
  222.     char *copy_string();
  223.  
  224.     if ( addsym( copy_string( nd ), copy_string( def ), 0,
  225.          ndtbl, NAME_TABLE_HASH_SIZE ) )
  226.     synerr( "name defined twice" );
  227.     }
  228.  
  229.  
  230. /* ndlookup - lookup a name definition
  231.  *
  232.  * synopsis
  233.  *    char nd[], *def;
  234.  *    char *ndlookup();
  235.  *    def/NULL = ndlookup( nd );
  236.  */
  237.  
  238. char *ndlookup( nd )
  239. char nd[];
  240.  
  241.     {
  242.     return ( findsym( nd, ndtbl, NAME_TABLE_HASH_SIZE )->str_val );
  243.     }
  244.  
  245.  
  246. /* scinstal - make a start condition
  247.  *
  248.  * synopsis
  249.  *    char str[];
  250.  *    int xcluflg;
  251.  *    scinstal( str, xcluflg );
  252.  *
  253.  * NOTE
  254.  *    the start condition is Exclusive if xcluflg is true
  255.  */
  256.  
  257. scinstal( str, xcluflg )
  258. char str[];
  259. int xcluflg;
  260.  
  261.     {
  262.     char *copy_string();
  263.  
  264.     /* bit of a hack.  We know how the default start-condition is
  265.      * declared, and don't put out a define for it, because it
  266.      * would come out as "#define 0 1"
  267.      */
  268.     /* actually, this is no longer the case.  The default start-condition
  269.      * is now called "INITIAL".  But we keep the following for the sake
  270.      * of future robustness.
  271.      */
  272.  
  273.     if ( strcmp( str, "0" ) )
  274.     printf( "#define %s %d\n", str, lastsc );
  275.  
  276.     if ( ++lastsc >= current_max_scs )
  277.     {
  278.     current_max_scs += MAX_SCS_INCREMENT;
  279.  
  280.     ++num_reallocs;
  281.  
  282.     scset = reallocate_integer_array( scset, current_max_scs );
  283.     scbol = reallocate_integer_array( scbol, current_max_scs );
  284.     scxclu = reallocate_integer_array( scxclu, current_max_scs );
  285.     sceof = reallocate_integer_array( sceof, current_max_scs );
  286.     scname = reallocate_char_ptr_array( scname, current_max_scs );
  287.     actvsc = reallocate_integer_array( actvsc, current_max_scs );
  288.     }
  289.  
  290.     scname[lastsc] = copy_string( str );
  291.  
  292.     if ( addsym( scname[lastsc], (char *) 0, lastsc,
  293.          sctbl, START_COND_HASH_SIZE ) )
  294.     lerrsf( "start condition %s declared twice", str );
  295.  
  296.     scset[lastsc] = mkstate( SYM_EPSILON );
  297.     scbol[lastsc] = mkstate( SYM_EPSILON );
  298.     scxclu[lastsc] = xcluflg;
  299.     sceof[lastsc] = false;
  300.     }
  301.  
  302.  
  303. /* sclookup - lookup the number associated with a start condition
  304.  *
  305.  * synopsis
  306.  *    char str[], scnum;
  307.  *    int sclookup;
  308.  *    scnum/0 = sclookup( str );
  309.  */
  310.  
  311. int sclookup( str )
  312. char str[];
  313.  
  314.     {
  315.     return ( findsym( str, sctbl, START_COND_HASH_SIZE )->int_val );
  316.     }
  317.